#ifndef RENDERWIDGET_H
#define RENDERWIDGET_H

#include <QWidget>
#include <QOpenGLWidget>
#include <QOpenGLShader>
#include <QOpenGLShaderProgram>
#include <QOpenGLTexture>
#include <QOpenGLFunctions_3_3_Core>
#include <QDebug>
#include <QImage>
#include <QEvent>
#include <QTime>
#include <QKeyEvent>
#include<QVector>
#include "shader.h"
#include "camera.h"
#include "rendertarget.h"
#include "GameUI/gamemainwindow.h"
#include "Narration/narrationnode.h"
#include "GameUI/option.h"
#include "UIEditor/uilib.h"
#include "GameUI/gameuicontainer.h"
#include "Render/particlesystem.h"
QT_BEGIN_NAMESPACE
namespace Ui { class RenderWidget; }
QT_END_NAMESPACE

class RenderWidget: public QOpenGLWidget
{
    Q_OBJECT

public:
    explicit RenderWidget(QWidget *parent = nullptr);
    ~RenderWidget();
    enum CursorState{
        //MOVE_SCREEN,
        MOVE_IMAGE,
        SELECT,
        RUN,//运行游戏时
    };
    RenderTarget* chosen = nullptr;
    //宽高
    QSize winSize = QSize(1200.0f,800.0f);
    Option* GetOptionWidget();
    void SetParticleDisabled(bool disable);
    void SetDialog(QString text,QString name);
    void ClearRenderList();
    void AddRenderTarget(RenderTarget* rt,bool adjust_layer = false,bool force_override = false);
protected:

    virtual void initializeGL();
    virtual void paintGL();
    virtual void resizeGL(int w, int h);
    //画正方形

//callback functions about keyboard and mouse
    void keyPressEvent(QKeyEvent *event);
    void keyReleaseEvent(QKeyEvent *event);
    void mouseMoveEvent(QMouseEvent *event);
    void mousePressEvent(QMouseEvent *event);
    void MoveImageMousePress(QMouseEvent *event);
    void SelectMousePress(QMouseEvent *event);
    void mouseReleaseEvent(QMouseEvent *event);
    void MoveImageMouseReleaseEvent(QMouseEvent *event);
    void SelectMouseReleaseEvent(QMouseEvent *event);

    virtual void dragEnterEvent(QDragEnterEvent *event);
    virtual void dropEvent(QDropEvent *event);

private:
    Ui::RenderWidget *ui;

    QOpenGLFunctions_3_3_Core *core;
    QOpenGLShaderProgram shaderProgram;
    Camera *camera;
    GLuint VAO;
    GLuint VBO;
    GLuint VBOTexturePos;
    QOpenGLTexture *outLineTexture;

    QMap<int,RenderTarget*> renderList;
    //当第二个参数为true时，如果其layer上已经有图片，则会递增适应。为false时直接添加失败
    QVector<QOpenGLTexture*> textures;
    Shader *myShader;

    CursorState state = SELECT;

    void Init();
    void InitVAOandVBO();
    //layer越小就渲染在越前面（遮挡其他的）,每张图片不可渲染在同一层
    void Draw(RenderTarget* rt);
    void DrawChosenOutline();

    void MoveImageState();
    void SelectState();

    QVector2D GetMouseNDC();
    QVector2D MoveImageOffset = QVector2D(0.0f,0.0f);

    //UI
    GameMainWindow* gameMainWindow;
    Option* option;
    void InitUI();

    //是否播放粒子效果
    bool activeParticle = false;
    ParticleSystem *ps = nullptr;
    void DrawParticle();
    QVector<Particle*> particles;

    //使帧率保持在60左右
    QTimer *timer = nullptr;
signals:
    void ChosenChange(RenderTarget* rt);
    void RefreshChosen();
public slots:
    void CursorChange(CursorState state);

};

#endif // RENDERWIDGET_H
